package com.dindang.checkline.mvp.model;

import android.app.Application;

import com.dindang.checkline.mvp.model.api.service.CommonService;
import com.dindang.checkline.mvp.model.entity.BaseResponse;
import com.dindang.checkline.mvp.model.entity.CheckLineEntity;
import com.dindang.checkline.mvp.model.entity.ItemDetailEntity;
import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.dindang.checkline.mvp.contract.CheckingContract;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 03/07/2019 15:01
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class CheckingModel extends BaseModel implements CheckingContract.Model {
    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public CheckingModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public Observable<BaseResponse> submitItemDetail(ItemDetailEntity param){
        String[] imgs = param.getImg().split(",");
        List<File> files = new ArrayList<>();
        List<InputStream> inputStreams = new ArrayList<>();
        List<byte[]> bytes = new ArrayList<byte[]>();
        for (int i = 0; i < imgs.length; i++) {
            File item = new File(imgs[i]);
            try {
                InputStream inputStream = new FileInputStream(item);
                inputStreams.add(inputStream);
                byte[] byteItem =  new byte[inputStream.available()];
                bytes.add(byteItem);
            } catch (Exception e) {
                e.printStackTrace();
            }
            files.add(item);
        }
        Observable<BaseResponse> base = mRepositoryManager.obtainRetrofitService(CommonService.class)
                .submitItemDetail(param.itemId+"",
                        param.status,param.unomalDescribe,
                        bytes,param.remarks,param.checkTime+"",param.userid
                );
        return base;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }
}